<template>
  <div>
    <header-tab>
      <span slot="left" @click="Go_back">
        <van-icon name="arrow-left" />
      </span>
      <span slot="title">伴宠日记</span>
    </header-tab>
    <van-list v-model="loading" :finished="finished" finished-text="我是有底线的~" @load="onLoad">
      <div @click="go_dailydetial(item)" class="card" v-for="(item, d_id) in CardItems" :key="d_id">
        <div class="card-word">
          <div class="card-word-left">
            <img class="user_touxiang" :src="item.d_img" alt="" />
            <!-- 用户名 -->
            <span class="user_name">{{ item.d_man_name }}</span>
          </div>
          <!-- 用户头像 -->

          <!-- 地点 -->
          <span class="user_city">{{ item.d_city }}</span>
        </div>
        <div class="card-img">
          <!-- 文章详情 -->
          <p>{{ item.d_detail }}</p>
          <!-- 文章配图 -->

          <img :src="item.d_img" alt="" />
        </div>
      </div>
    </van-list>
  </div>
</template>

<script>
import HeaderTab from '@/components/HeaderTab.vue';

export default {
  components: { HeaderTab },

  data() {
    return {
      CardItems: [],
      pageNum: 0, //用于limit查询 从第一个元素开始查询
      pageSize: 5, //每次查询5个
      // List 组件通过 loading 和 finished 两个变量控制加载状态，当组件滚动到底部时，会触发 load 事件并将 loading 设置成 true。此时可以发起异步操作并更新数据，数据更新完毕后，将 loading 设置成 false 即可。若数据已全部加载完毕，则直接将 finished 设置成 true 即可。
      loading: false,
      finished: false,
    };
  },
  methods: {
    Go_back() {
      this.$router.go(-1);
    },

    // 获取三条最新日记数据
    onLoad() {
      const url = 'dairy/selectAll';
      let params = `pageNum=${this.pageNum}&&pageSize=${this.pageSize}`;
      this.axios.post(url, params).then(res => {
        if (res.data.code == 201) {
          this.finished = true;
        }
        console.log(res);
        this.CardItems.push(...res.data.data);
        this.pageNum += 5;
        // 加载状态结束
        this.loading = false;
        // 数据全部加载完成
      });
    },

    // 跳转至日记详情
    go_dailydetial(item) {
      this.$router.push({
        name: 'deilydetial', //跳转至 deilydetial 路由
        query: {
          d_id: item.d_id,
        },
      });
      this.$route.query.name;
    },
  },
};
</script>
<style lang="scss" scoped>
.card {
  color: #36343c;
  background-color: white;
  width: 90%;
  height: 30rem;
  margin: 2rem auto;
  border-radius: 1rem;
  box-shadow: 0 0.4rem 0.8rem 0 rgba(0, 0, 0, 0.2), 0 0.6rem 2rem 0 rgba(0, 0, 0, 0.19);

  .card-word {
    clear: both;
    width: 90%;
    display: flex;
    justify-content: space-between;

    align-items: center;
    margin: auto;
    padding-top: 2rem;
    .card-word-left {
      width: 25rem;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .user_touxiang {
        display: block;
        width: 3rem;
        height: 3rem;
        //裁减掉多余部分,不拉伸 不失真
        object-fit: cover;
        border-radius: 50%;
        background-color: white;
      }

      .user_name {
        margin-left: 1.5rem;
        display: inline-block;
        font-weight: bold;
        font-size: 1.6rem;
      }
    }

    .user_city {
      color: #36343c;
    }
  }

  .card-img {
    width: 90%;
    height: 90%;
    margin: 0 auto;
    p {
      margin-top: 1rem;
      font-size: 1.5rem;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    img {
      margin-top: 1rem;
      width: 100%;
      height: 70%;
      border-radius: 1rem;
      //裁减掉多余部分,不拉伸 不失真
      object-fit: cover;
    }
  }
}
</style>
